import api from '../../api/api'
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: {}
  },

  async findAccountInfo() {
    const res = await api.getPersionInfo({ tokenString: wx.getStorageSync('token') })
    console.log('个人信息', res);
    this.setData({
      result: res.data.result[0]
    })
  },
  async updateNickName(e) {
    let nickName = e.detail.value
    const res = await api.updateNickName({ tokenString: wx.getStorageSync('token'), nickName })
    if (res.data.code == "C001") {
      console.log('修改昵称', res)
      Toast.success(res.data.msg)
      this.findAccountInfo()
    }
  },
  async updateDesc(e) {
    let desc = e.detail.value
    const res = await api.updateDesc({ tokenString: wx.getStorageSync('token'), desc })
    console.log(res);
    if (res.data.code == "D001") {
      console.log('修改简介', res)
      Toast.success(res.data.msg)
      this.findAccountInfo()
    }
  },
  logout() {
    Dialog.confirm({
      // title: '标题',
      message: '确定要退出登录吗？',
    }).then(async () => {
      const res = await api.logout({ tokenString: wx.getStorageSync('token') })
      console.log('退出登录', res)
      Toast.success(res.data.msg)
      wx.clearStorage()
      setTimeout(() => {
        app.checkLogin()
      }, 2000)
    }).catch(() => {
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.findAccountInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})